﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;

namespace CC.Inet
{

    public class Email
    {
        /// <summary>
        /// 发送者
        /// </summary>
        public string Sender { get; set; }

        /// <summary>
        /// 收件人
        /// </summary>
        public List< string> Receivers { get; set; }

        /// <summary>
        /// 抄送
        /// </summary>
        public List<string> CCReveivers { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string MailSubject { get; set; }

        /// <summary>
        /// 正文
        /// </summary>
        public string MailBody { get; set; }

        /// <summary>
        /// 发件人密码
        /// </summary>
        public string MailPwd { get; set; }

        /// <summary>
        /// SMTP邮件服务器
        /// </summary>
        public string SmtpServer { get; set; }
        public int ServerPort { get; set; }
        public bool EnableSSL { get; set; }
        public bool UseDefaultCredentials { get; set; }

        /// <summary>
        /// 正文是否是html格式
        /// </summary>
        public bool IsbodyHtml { get; set; }

        /// <summary>
        /// 附件
        /// </summary>
        public List<string> Attachments { get; set; }

        public Email()
        {
            Receivers = new List<string>();
            CCReveivers = new List<string>();
            Attachments = new List<string>();
        }
        public bool Send()
        {
            //使用指定的邮件地址初始化MailAddress实例
            MailAddress maddr = new MailAddress(Sender);
            //初始化MailMessage实例
            MailMessage myMail = new MailMessage
            {
                //发件人地址
                From = maddr,

                //电子邮件的标题
                Subject = MailSubject,

                //电子邮件的主题内容使用的编码
                SubjectEncoding = Encoding.UTF8,

                //电子邮件正文
                Body = MailBody,

                //电子邮件正文的编码
                BodyEncoding = Encoding.Default,

                Priority = MailPriority.High,

                IsBodyHtml = IsbodyHtml
            };

            //向收件人地址集合添加邮件地址
            foreach (var receiver in Receivers)
            {
                myMail.To.Add(receiver);
            }
           
            //向抄送收件人地址集合添加邮件地址
            if (CCReveivers .Count>0)
            {
                foreach (var cc in CCReveivers)
                {
                    myMail.CC.Add(cc);
                }
            }
            

            //在有附件的情况下添加附件
            try
            {
                if ( Attachments.Count > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in Attachments)
                    {
                        attachFile = new Attachment(path);
                        myMail.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("在添加附件时有错误:" + err);
            }

            SmtpClient smtp = new SmtpClient
            {
                //指定发件人的邮件地址和密码以验证发件人身份
                Credentials = new System.Net.NetworkCredential(Sender, MailPwd),
                //设置SMTP邮件服务器
                Host = SmtpServer,
                EnableSsl= EnableSSL,
                UseDefaultCredentials=true,
                Port=ServerPort,
               
            };
            //将邮件发送到SMTP邮件服务器
           
            smtp.Send(myMail);
            return true;

        }
       
    }
}
